import requests
import json
from com.demo1.ai.qianfan import demo_qf_ai_base_config as qfConfig
from PIL import Image
import io
import base64

# 图片文件路径
image_path = '/Users/fym/Desktop/3AF8EFAB-7B05-4819-89CE-E3E513A3018A.png'

# 打开图片文件
with Image.open(image_path) as img:
    # 将图片转换为字节流
    img_byte_arr = io.BytesIO()
    img.save(img_byte_arr, format='PNG')  # 这里假设图片是JPEG格式，根据实际情况调整
    img_byte_arr = img_byte_arr.getvalue()

# 将图片字节数据转换为Base64编码
img_base64 = base64.b64encode(img_byte_arr)

# 打印Base64编码的图片字符串
print(img_base64.decode('utf-8'))  # 解码为字符串以便打印或使用




def main():
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/image2text/fuyu_8b?access_token=" + qfConfig.get_access_token()

    payload = json.dumps({
        "prompt": "introduce the picture",
        "image": img_base64.decode('utf-8')
    })
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)


if __name__ == '__main__':
    main()
